﻿using System;
using System.Data;
using System.Xml;
using System.Xml.Linq;

namespace Savage.SecureData
{
    public class Securable : ISecurable
    {
        public Securable()
        { }

        public Securable(IDataReader reader)
        { 
            SecurableId = (Guid)reader["SecurableId"];
            SchemaName = (string)reader["SchemaName"];
            DocumentType = (DocumentType)Enum.Parse(typeof(DocumentType), (string)reader["DocumentType"], true);
            string data = reader["Data"] as string;
            if (!String.IsNullOrEmpty(data))
                Data = XDocument.Parse(data);
        }

        public Securable(Guid? securableId, string schemaName, DocumentType documentType, XDocument data)
        {
            SecurableId = securableId;
            SchemaName = schemaName;
            DocumentType = documentType;
            Data = data;
        }

        public Securable(string schemaName, DocumentType documentType, XDocument data) 
            : this(null, schemaName, documentType, data)
        {
        }

        public Securable(Guid? securableId, string schemaName, DocumentType documentType, string data)
        {
            SchemaName = schemaName;
            DocumentType = documentType;
            Data = XDocument.Parse(data);
        }

        public Securable(string schemaName, DocumentType documentType, string data)
            : this(null, schemaName, documentType, data)
        { }

        public Guid? SecurableId { get; set; }
        public string SchemaName { get; set; }
        public DocumentType DocumentType { get; set; }
        public XDocument Data { get; set; }

        public Securable ToSecurable()
        {
            return this;
        }
    }
}
